//
//  ScheduleDAO.m
//  GolfFriend
//
//  Created by vescky on 13-11-30.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "ScheduleDAO.h"
#import "DateUtility.h"

#define TB_SIMPLESCHEDULE @"tb_simple_schedule"

@implementation ScheduleDAO

static ScheduleDAO *defaultScheduleDAO;

+ (id)shareScheduleDAO {
    if (!defaultScheduleDAO) {
        defaultScheduleDAO = [[ScheduleDAO alloc] init];
        DB *dbase = [[DB alloc] init];
        FMDatabase *database = [dbase getDatabase];
        [database executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(recordId integer primary key,userId text,dateString text,timeString text,content text,status text)",TB_SIMPLESCHEDULE]];
        [database close];
    }
    [defaultScheduleDAO updateRecordStatus];
    return defaultScheduleDAO;
}

- (bool)saveScheduleDictionary:(ScheduleContentVo*)sContent {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:[sContent toDictionary]];
    [params removeObjectForKey:@"recordId"];
    
    if (!params || [params count] < 1) {
        return NO;
    }
    
    if (sContent.recordId && sContent.recordId.length > 0) {
        //有记录的，update
        [self update:params where:[[NSMutableDictionary alloc] initWithObjectsAndKeys:sContent.recordId,@"recordId",nil] inTable:TB_SIMPLESCHEDULE];
    }
    else {
        //无记录的，insert
        [self insert:params inTable:TB_SIMPLESCHEDULE];
    }
    
    return YES;
}

- (NSMutableArray*)getUserSchedule:(NSString*)userId {
    NSString *qConfition = [NSString stringWithFormat:@"select * from %@ where userId = '%@' and status <> '-1'",TB_SIMPLESCHEDULE,userId];//[NSString stringWithFormat:@"select * from %@ where userId = '%@' order by recordId desc limit 0,%d ",TB_SIMPLESCHEDULE,userId,lRows];
    return [self queryByConfition:qConfition];
}

- (bool)updateRecordStatus {
    NSString *qConfition = [NSString stringWithFormat:@"select * from %@ where status <> '-1'",TB_SIMPLESCHEDULE];
    NSDate *dateNow = getDateFromString(@"MM-dd", getStringFromDate(@"MM-dd", [NSDate date]));
    NSMutableArray *arr = [self queryByConfition:qConfition];
    for (int i = 0; i < [arr count]; i++) {
        NSMutableDictionary *_dict = [[NSMutableDictionary alloc] initWithDictionary:[arr objectAtIndex:i]];
        NSString *dateString =  [[_dict objectForKey:@"dateString"] stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
        NSDate *date1 = getDateFromString(@"MM-dd", dateString);
        if ([date1 isEqualToDate:dateNow]) {
            [_dict setObject:@"0" forKey:@"status"];
            ScheduleContentVo *sVo = [[ScheduleContentVo alloc] init];
            [sVo dictionaryToScheduleContentVO:_dict];
            [self saveScheduleDictionary:sVo];
            continue;
        }
        if ([dateNow isEqualToDate:[dateNow laterDate:date1]]) {
            [_dict setObject:@"-1" forKey:@"status"];
            ScheduleContentVo *sVo = [[ScheduleContentVo alloc] init];
            [sVo dictionaryToScheduleContentVO:_dict];
            [self saveScheduleDictionary:sVo];
        }
    }
    
    return YES;
}

@end
